package com.lovo.testjpa.dao;

import com.lovo.testjpa.bean.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface StudentDAO extends JpaRepository<Student,Integer>, JpaSpecificationExecutor {
    List<Student> findStudentByName(String name);

    @Query(value = "from Student where name=:name and age=:age")
    List<Student> listStudent(String name, Integer age);

    @Query(value = "from Student where gender=?1")
    Page<Student> listStudentByGender(String gender, Pageable pageable);

    @Query(value = "from Student where name=:#{#student.name}" +
            " and age=:#{#student.age}" +
            " and gender=:#{#student.gender}")
    List<Student> listStudentByCondition(Student student);

    @Modifying
    @Query(value = "delete from t_student where s_gender=:gender",nativeQuery = true)
    void deleteByGender(String gender);

    @Modifying
    @Query(value = "update t_student set s_c_id=null where s_c_id=:classId",nativeQuery = true)
    void updateByClassId(Integer classId);
}
